package com.yanqu.road.dao.impl.union.cross.huaben;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.huaben.UnionHuaBenUnionData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionHuaBenUnionDataDaoImpl extends TempDataObjectDao<UnionHuaBenUnionData> {

    public static String getUnionHuaBenUnionDataSql = "select * from `t_c_hua_ben_union` ";
    public Map<Long, Map<String, UnionHuaBenUnionData>> getUnionHuaBenUnionData() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, Map<String, UnionHuaBenUnionData>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getUnionHuaBenUnionDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionHuaBenUnionData data = parse(rs);
                if(result.get(data.getServerId()) == null){
                    result.put(data.getServerId(), new ConcurrentHashMap<>());
                }
                result.get(data.getServerId()).put(data.getUnionUid(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertUnionHuaBenUnionDataSql = "insert into `t_c_hua_ben_union`(unionUid,serverId,gradeMap) values (?,?,?)";

    @Override
    protected boolean addData(UnionHuaBenUnionData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGradeMap())));
        if (getDb().executeNoneQuery(insertUnionHuaBenUnionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUnionHuaBenUnionDataSql = "update `t_c_hua_ben_union` set serverId=?,gradeMap=? where unionUid=?";

    @Override
    protected boolean updateData(UnionHuaBenUnionData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGradeMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        if (getDb().executeNoneQuery(updateUnionHuaBenUnionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UnionHuaBenUnionData parseData(ResultSet rs) throws SQLException {
        UnionHuaBenUnionData data = new UnionHuaBenUnionData();
        data.setUnionUid(rs.getString("unionUid"));
        data.setServerId(rs.getLong("serverId"));
        data.setGradeMap(JSON.parseObject(rs.getString("gradeMap"), new TypeReference<Map<Integer, Map<Integer, Integer>>>() {}));
        return data;
    }
}
